package com.dkm.orproject.dkmoa.action;

import com.alibaba.fastjson.JSON;
import com.dkm.orproject.dkmoa.domain.OaLog;
import com.dkm.orproject.dkmoa.domain.OaOfficeSuppliesType;
import com.dkm.orproject.dkmoa.domain.OaPersonnel;
import com.dkm.orproject.dkmoa.domain.OaRegularFlow;
import com.dkm.orproject.dkmoa.service.OaOfficeSuppliesTypeService;
import com.dkm.orproject.utils.jwt.login.UserLoginToken;
import com.dkm.orproject.utils.utils.Tools;
import io.swagger.annotations.*;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 *  @ClassName : OaOfficeSuppliesTypeAction  //类名
 *  @Description : 办公用品  //描述
 *  @Author : 石智诚  //作者
 *  @Date: 2020-11-16 10:15  //时间
 */
@RestController
@RequestMapping("/SuppliesTypeAction")
public class OaOfficeSuppliesTypeAction {
    /**
     * 办公用品类型
     */
    @Resource
    OaOfficeSuppliesTypeService typeService;
    /**
     * 工具类
     */
    @Resource
    Tools tools;
    /**
     * 日志
     */
    OaLog log;
    /**
     * 登陆人
     */
    OaPersonnel person;

    /**
     * 回调map
     */
    Map<String,Object> map;
    /**
     * 行数
     */
    int row;
    @UserLoginToken
    @GetMapping("typeSelectAll")
    public Map<String, Object> selectAll(OaOfficeSuppliesType suppliesType,
                                         String startDate,String endDate,
                                         Integer page,Integer limit,HttpServletRequest request){
        try {
            List<OaOfficeSuppliesType> suppliesTypes = typeService.selectAll(suppliesType, startDate, endDate, page, limit);
            Long count = typeService.rowCount(suppliesType, startDate, endDate);
            return Tools.maps(suppliesTypes,count);
        } catch (Exception e) {
          addErrorLog(request,"办公用品类型查询",e);
        }
    return  null;
    }

    /**
     * 根据办公用品类型Id查询
     * @param id 办公用品类型Id
     * @param request 请求
     * @return map
     */
    @ApiOperation(value = "办公用品类型id", notes = "办公用品类型id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "办公用品类型id",dataType = "Long",paramType = "path"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "String", paramType = "header",required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = OaRegularFlow.class),
    })

    @UserLoginToken
    @GetMapping("typeFindById/{id}")
    public Map<String, Object> findById(@PathVariable("id") Long id,HttpServletRequest request){
        rowMap(request);
        if (ObjectUtils.isEmpty(person)){
            tools.rowMap(map,0,log);
        }
        try {
            OaOfficeSuppliesType oaOfficeSuppliesType = typeService.selectByTypeId(id);
            map.put("data",oaOfficeSuppliesType);
        }catch (Exception e){
            addErrorLog(request,"办公用品类型查找",e);
        }
        return map;
    }

    @UserLoginToken
    @GetMapping("selectType")
    public Map<String, Object> selectType(Long id, HttpServletRequest request){
        rowMap(request);
        if (ObjectUtils.isEmpty(person)){
            tools.rowMap(map,0,log);
        }
        try {
            List<OaOfficeSuppliesType> types = typeService.selectType(id);
            map.put("data",types);
        }catch (Exception e){
            addErrorLog(request,"办公用品类型(下拉框)",e);
        }
        return map;
    }

    /**
     * 办公用品类型更新或者增加
     * @param suppliesType 办公用品类型
     * @param request 请求
     * @return 模型
     */
    @ApiOperation(value = "办公用品类型更新或者更新", notes = "办公用品类型更新或者更新")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "batchOvertimeDetailStart",value = "加班开始时间",dataType = "Date",paramType = "query"),
            @ApiImplicitParam(name = "batchOvertimeDetailEnd",value = "加班结束时间",dataType = "Date",paramType = "query"),
            @ApiImplicitParam(name = "batchOvertimeDetailHour",value = "小时数",dataType = "decimal",paramType = "query"),
            @ApiImplicitParam(name = "batchOvertimeDetailCause",value = "加班事由",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "personnelId",value = "申请人id",dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "String", paramType = "header",required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "提交成功"),
    })
    @UserLoginToken
    @PostMapping("typeAddOrUpdate")
    public Map<String, Object>addOrUpdate(OaOfficeSuppliesType suppliesType,HttpServletRequest request){
        rowMap(request);
        if (ObjectUtils.isEmpty(person)){
            tools.rowMap(map,0,log);
        }
        try {
            if (suppliesType.getPkOfficeSuppliesTypeId()!=null&&suppliesType.getPkOfficeSuppliesTypeId()>0) {
              suppliesType.setUpdatedBy(person.getPersonnelName());
              suppliesType.setUpdatedTime(new Date());
              row = typeService.updateByTypeId(suppliesType);
              log.setLogTitle("办公用品类型更新");
            } else {
                suppliesType.setCreatedBy(person.getPersonnelName());
                suppliesType.setCreatedTime(new Date());
                row= typeService.insert(suppliesType);
                log.setLogTitle("办公用品类型增加");
            }
            log.setLogBody(JSON.toJSON(suppliesType).toString());
            log.setIsRevision("1");
            tools.rowMap(map,row,log);
        } catch (Exception e) {
           addErrorLog(request,"办公用品类型增加或者更新",e);
        }
        return  map;
    }

    /**
     * 根据办公用品类型删除
     * @param id 办公用品类型Id
     * @param request 请求
     * @return map
     */
    @ApiOperation(value = "id删除", notes = "id删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "办公用品类型Id",dataType = "Long",paramType = "path"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "String", paramType = "header",required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "删除成功"),
    })
    @UserLoginToken
    @DeleteMapping("typeDeleteById/{id}")
    public Map<String, Object> deleteById(@PathVariable(value = "id",required = false)Long id, HttpServletRequest request){
        rowMap(request);
        if (ObjectUtils.isEmpty(person)) {
            tools.rowMap(map,0,log);
        }
        try {
            OaOfficeSuppliesType type = typeService.selectByTypeId(id);
            row = typeService.deleteByTypeId(id);
            log.setLogBody(JSON.toJSON(type).toString());
            log.setLogType("1");
            tools.rowMap(map, row, log);
        } catch (Exception e) {
           addErrorLog(request,"办公用品类型删除",e);
        }
        return map;
    }
    /**
     * 根据办公用品类型删除
     * @param ids 办公用品类型Id
     * @param request 请求
     * @return map
     */
    @ApiOperation(value = "批量删除", notes = "批量删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "办公用品类型Id",dataType = "Long[]",paramType = "path"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "String", paramType = "header",required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "删除成功"),
    })
    @DeleteMapping("typeDeleteAll")
    public Map<String ,Object> deleteAll(@RequestParam(value = "ids[]",required = false)Long[] ids,HttpServletRequest request){
        rowMap(request);
        if (ObjectUtils.isEmpty(person)){
           tools.rowMap(map,0,log);
        }
        try {
            List<Long> list = Arrays.asList(ids);
            List<OaOfficeSuppliesType> all = typeService.findAll(list);
            row = typeService.deleteAll(list);
            log.setLogBody(JSON.toJSON(all).toString());
            log.setLogTitle("办公用品类型批量删除");
            log.setIsRevision("1");
            tools.rowMap(map,row,log);
        } catch (Exception e) {
            addErrorLog(request,"办公用品类型批量删除",e);
        }
        return  map;
    }

    /**
     * 简化代码
     * @param request 请求
     */
    private void addErrorLog(HttpServletRequest request, String title, Exception e){
        OaPersonnel personnel= (OaPersonnel) request.getSession().getAttribute("user");
        log=new OaLog();
        log.setLogType("0");
        log.setLogBody(e.getLocalizedMessage());
        log.setLogTitle(title);
        log.setLogIp(request.getRemoteAddr());
        if(ObjectUtils.isEmpty(personnel)){
            log.setCreatedBy(personnel.getPersonnelName());
        }
        tools.errorMap(log);
    }

    private void rowMap(HttpServletRequest request){
        //新建返回Map模型
        map=new HashMap<>(16);
        //获取登陆人信息
        person= (OaPersonnel) request.getSession().getAttribute("user");
        //新建日志
        log=new OaLog();
        //得到ip
        log.setLogIp(request.getRemoteAddr());
        //登陆人名称
        log.setCreatedBy(person.getPersonnelName());
    }
}
